﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using FisheryPlatform.DataAccess.DBUtility;
using System.Data;

namespace FisheryPlatform.DataAccess.Passport
{
    public class VerificationCodeDB
    {
        #region private const
        private const string PARAM_GUID_ID = "@GuidId";
        private const string PARAM_CODE = "@Code";
        private const string PARAM_CREATE_DATE = "@CreateDate";

        private static readonly string ConnectionString = SQLHelper.CONNECTION_STRING;
        #endregion

        public static string Insert(string code)
        {
            #region
            string guid = string.Empty;
            SqlParameter[] parameterLst = new SqlParameter[]
            {
                SQLHelper.MakeOutParam(PARAM_GUID_ID,SqlDbType.UniqueIdentifier,0,null),
                SQLHelper.MakeParam(PARAM_CODE,SqlDbType.VarChar,0,code)
            };
            try
            {
                SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "VerificationCode_Insert", parameterLst);
                guid = parameterLst[0].Value.ToString();
            }
            catch (Exception e)
            {
                Exception ex = new Exception("VerificationCodeDB-->Insert-->" + e.ToString());

                ExceptionMessageDB.Record(ex);
            }
            return guid;
            #endregion
        }

        /// <summary>
        /// 验证用户输入的验证码
        /// </summary>
        /// <param name="guid">客户端cookie中的guid</param>
        /// <param name="code">验证码</param>
        /// <returns>true为通过验证,false为未通过验证</returns>
        public static bool Validate(Guid guid, string code)
        {
            #region
            bool result = false;
            try
            {
                SqlParameter[] parameterLst = new SqlParameter[]
                    {
                        SQLHelper.MakeParam(PARAM_GUID_ID,SqlDbType.UniqueIdentifier,0,guid),
                        SQLHelper.MakeParam(PARAM_CODE,SqlDbType.VarChar,0,code)
                    };
                object value = SQLHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "VerificationCode_Validate", parameterLst);
                int intValue = Convert.ToInt32(value);
                if (intValue == 1)
                {
                    result = true;
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("VerificationCodeDB-->Validate-->" + e.ToString());

                ExceptionMessageDB.Record(ex);
            }

            return result;
            #endregion
        }

    }
}
